//
//  LoginAndRegisterViewController.m
//  BaChat
//
//  Created by 刘洋 on 16/6/13.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "LoginAndRegisterViewController.h"
#import "RegisterViewController.h"
#import "LabelAndTextView.h"
#import "BaseTabBarController.h"
#import "AppDelegate.h"



@interface LoginAndRegisterViewController ()

@property(nonatomic,retain)BaseLabel *welcomeLabel;

@property(nonatomic,retain)LabelAndTextView *nameView;
@property(nonatomic,retain)LabelAndTextView *passwordView;

@property(nonatomic,retain)BaseButton *registerButton;
@property(nonatomic,retain)BaseButton *loginButton;
@end

@implementation LoginAndRegisterViewController

-(void)dealloc{
    [_welcomeLabel release];
    [_nameView release];
    [_passwordView release];
    [super dealloc];
}





- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.welcomeLabel = [[BaseLabel alloc]initWithFrame:CGRectMake(0, ROOTHEIGHT / 5, ROOTWIDTH, 40)];
    self.welcomeLabel.backgroundColor = [UIColor whiteColor];
    self.welcomeLabel.text = @"使用帐号和密码登录";
    self.welcomeLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.welcomeLabel];
    [_welcomeLabel release];
    
  
    self.nameView = [[LabelAndTextView alloc]initWithFrame:CGRectMake(ROOTWIDTH / 15, self.welcomeLabel.frame.origin.y + self.welcomeLabel.frame.size.height + 40, ROOTWIDTH / 4 * 3 + 30, 40)];
    self.nameView.mylabel.text = @"帐号";
    self.nameView.myTextField.placeholder = @"    用户名/手机号";
    [self.view addSubview:self.nameView];
    [_nameView release];
    
    self.passwordView = [[LabelAndTextView alloc]initWithFrame:CGRectMake(self.nameView.frame.origin.x, self.nameView.frame.origin.y + self.nameView.frame.size.height + 20, self.nameView.frame.size.width, self.nameView.frame.size.height)];
    self.passwordView.mylabel.text = @"密码";
    self.passwordView.myTextField.placeholder = @"    请填写密码";
    self.passwordView.myTextField.secureTextEntry = YES;
    [self.view addSubview:self.passwordView];
    [_passwordView release];
    
    
    
    BaseButton *loginButton = [BaseButton buttonWithType:UIButtonTypeCustom];
    loginButton.frame = CGRectMake(ROOTWIDTH / 8, self.passwordView.frame.origin.y +self.passwordView.frame.size.height + ROOTHEIGHT / 15, ROOTWIDTH - ROOTWIDTH / 8 * 2, 40);
    [loginButton setBackgroundImage:[UIImage imageNamed:@"login_gray"] forState:UIControlStateNormal];
    [self.view addSubview:loginButton];
    self.loginButton = loginButton;
    
    //取消图片Button的点击效果
    loginButton.adjustsImageWhenHighlighted = NO;
    [loginButton addTarget:self action:@selector(loginButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.registerButton = [BaseButton buttonWithType:UIButtonTypeCustom];
    self.registerButton.frame = CGRectMake(ROOTWIDTH / 2 - ROOTWIDTH / 8 / 2, ROOTHEIGHT - ROOTHEIGHT / 10,ROOTWIDTH / 8 ,30) ;
    self.registerButton.backgroundColor = [UIColor clearColor];
    [self.registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [self.registerButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.view addSubview:self.registerButton];
    [self.registerButton addTarget:self action:@selector(registerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
//监听输入内容的情况
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(buttonSelected:) name:UITextFieldTextDidChangeNotification object:nil];
    
    
    
    
    
    // Do any additional setup after loading the view.
}

#pragma mark -- textFiel改变时通知button改变样式
-(void)buttonSelected:(NSNotification *)noti{
    
    NSString *name = self.nameView.myTextField.text;
    NSString *password = self.passwordView.myTextField.text;

    
    //任意一个为空,注册按钮都是暗的没有点击效果
    if (name.length == 0 || password.length == 0 ) {
        
        self.loginButton.userInteractionEnabled = NO;
        [self.loginButton setBackgroundImage:[UIImage imageNamed:@"login_gray"] forState:UIControlStateNormal];

    }else{
    //都不为空,注册按钮变亮可以注册
        self.loginButton.userInteractionEnabled = YES;
        [self.loginButton setBackgroundImage:[UIImage imageNamed:@"login_light"] forState:UIControlStateNormal];
        
    }
    
    
    
    
}



#pragma mark -- 登录按钮
-(void)loginButton:(BaseButton *)button{
    
    NSString *name = self.nameView.myTextField.text;
    NSString *password = self.passwordView.myTextField.text;
    
   [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:name password:password completion:^(NSDictionary *loginInfo, EMError *error) {
       if (!error) {
           NSLog(@"登录成功");
           
           //设置为自动登录
           [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:YES];
           
           BaseTabBarController *tabBarVC = [[BaseTabBarController alloc]init];
           
           AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
           
           app.window.rootViewController = tabBarVC;
           
       }else{
//登录失败,只能有一个原因是 帐号或密码错误
           MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
           hud.mode = MBProgressHUDModeCustomView;
           hud.labelText = @"帐号或密码错误";
           [hud hide:YES afterDelay:1];
           NSLog(@"%@",loginInfo);
           
       }
       
       
       
       
   } onQueue:dispatch_get_main_queue()];

    
    
    
    
}





#pragma mark -- 注册按钮
-(void)registerButtonAction:(BaseButton *)button{
    
    RegisterViewController *registerVC = [[RegisterViewController alloc]init];
    UINavigationController *registerNaVC = [[UINavigationController alloc]initWithRootViewController:registerVC];
    registerVC.passValue = ^(NSString *name, NSString *password){
        
        self.nameView.myTextField.text = name;
        self.passwordView.myTextField.text = password;
        
        
    };
    
    [registerVC release];
    [self presentViewController:registerNaVC animated:YES completion:^{
        
        
    }];
    [registerNaVC release];
    
    
    
    
}



-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.nameView.myTextField resignFirstResponder];
    [self.passwordView.myTextField resignFirstResponder];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
